<?php
/**
 * 护理记录功能相关接口模型
 * @authors Hex (hex@zpdchina.com)
 * @date    2017-03-09 19:19:32
 * @version $Id$
 */
class HuliWenshuDetailModel extends BaseModel
{
    public $_field_arr = ['id','zhuyuan_id','type','huli_wenshu_id','content','jilu_hushi_name',
                          'jilu_hushi_id','jilu_time','relate_tizheng_id','upload_state','is_update',
                          'jilu_state','update_time','bqgc_hlcs','deleted_at','zhen_duan','upload_time',
                          'upload_result','yiyuan_id'];
                          //'upload_result','yiyuan_id','jilu_hushi_sign_img'];

    public function addData($data = array())
    {
        $field_arr = $this->_field_arr;
        for ($i = 1; $i <= 60; $i++) {
            $field_arr[] = 'valcol'.$i;
        }
        $value_str = '(';
        foreach ($field_arr as $v) {
            // 拼接要插入的值
            $data[$v] = $data[$v] ?: '';
            if($data[$v]) {
                $new_field_arr[] = $v;
                $value_str .= '\'' . $data[$v] . '\',';
            }
        }
        // 去除最后一个逗号
        $value_str = rtrim($value_str, ',');
        $value_str .= ')';
        $new_field_str =  implode(',', $new_field_arr);
        // 拼接sql语句
        $sql = "INSERT INTO zhuyuan_huli_wenshu_detail ($new_field_str) VALUES $value_str";

        return DB::query(Database::INSERT, $sql)->execute();
    }

    public function getHuliList($zhuyuan_id,$yiyuan_id,$type,$start_riqi,$end_riqi,$jilu_state)
    {
        $kaishi = ($start_riqi?:date('Y-m-d')) . ' 00:00:00';
        $jieshu = ($end_riqi ?: date('Y-m-d')) . ' 23:59:59';
        $query = DB::select('id,jilu_hushi_name,type,jilu_time,jilu_state,jilu_hushi_id as user_number')
            ->from('zhuyuan_huli_wenshu_detail')
            ->where('zhuyuan_id','=',$zhuyuan_id)
            ->where('yiyuan_id','=',$yiyuan_id)
            ->where('jilu_time','>=',$kaishi)
            ->where('jilu_time','<=',$jieshu)
            ->where('deleted_at','IS', NULL);
        if (!empty($type)) {
            $query->where('type', '=', $type);
        }
        if(isset($jilu_state))
        {
            $query->where('jilu_state','=',$jilu_state);
        }else{
            $query->where('jilu_state','IS NOT', NULL);
        }
        $hulijilu = $query->execute()->as_array();
        $data['count'] = count($hulijilu);
        $data['result'] = $hulijilu;
        return $data;
    }

    //获取一条护理记录详情
    public function getHuliInfo($id,$hulijilus_field)
    {
        $data = DB::select($hulijilus_field)
            ->from('zhuyuan_huli_wenshu_detail')
            ->where('id','=',$id)
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->execute()
            ->current();
        return $data;
    }

    //获取护理记录单对应的多编辑数据
    public function getMultiInfo($id,$multi_field = [])
    {
        $query = DB::select('b.wenshu_id as hulijilu_id,b.content AS valcol,b.basic_info,b.other_info,b.last_info,b.remark')
            ->from(array('zhuyuan_huli_wenshu_detail', 'a'))
            ->join(array('zhuyuan_huli_wenshu_detail_content', 'b'), 'LEFT')
            ->on('a.id', '=', 'b.wenshu_id')
            ->on('a.yiyuan_id', '=', 'b.yiyuan_id')
            ->where('a.id','=',$id)
            ->where('a.yiyuan_id', '=', $this->yiyuan_id);
        if($multi_field)
            $query->where('b.content','IN',$multi_field);
        $data = $query->execute()->as_array();
        return $data;
    }

    //获取护理记录单对应的多编辑升级数据
    public function getMultiUpgradeInfo($id,$hulicol_name)
    {
        return DB::select('*')
            ->from('zhuyuan_huli_wenshu_detail_content_extend')
            ->where('wenshu_id','=',$id)
            ->where('content','=',$hulicol_name)
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->execute()->as_array();
    }

    public function getSubName($template_id){
        return DB::select('s.*')
            ->from(array('data_hulijilu_template_sub', 's'))
            ->join(array('data_hulijilu_template_sub_extend', 'e'),'LEFT')
            ->on('s.id', '=', 'e.sub_template_id')
            ->on('s.yiyuan_id', '=', 'e.yiyuan_id')
            ->where('e.template_id', '=', $template_id)
            ->where('s.yiyuan_id', '=',  $this->yiyuan_id)
            ->execute()
            ->as_array();
    }

    //获取某个患者某个表单类型的护理记录
    public function getHuliInfos($zhuyuan_id,$type,$jilu_time)
    {
        return DB::select('*')
            ->from('zhuyuan_huli_wenshu_detail')
            ->where('zhuyuan_id','=',$zhuyuan_id)
            ->where('type','=',$type)
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->where('DATE(jilu_time)','=', date('Y-m-d',strtotime($jilu_time)))
            ->execute()
            ->as_array();
    }

    public function updateInfos($zhuyuan_id,$type,$jilu_time)
    {
        return DB::update('zhuyuan_huli_wenshu_detail')
            ->set(array('jilu_state'  => 0))
            ->where('zhuyuan_id','=',$zhuyuan_id)
            ->where('type','=',$type)
            ->where('yiyuan_id', '=', $this->yiyuan_id)
            ->where('DATE(jilu_time)','=', date('Y-m-d',strtotime($jilu_time)))
            ->execute();
    }
    
    //删除数据
    public function delhulijilu($hulijilu_id){
    	DB::delete('zhuyuan_huli_wenshu_detail_content_extend')->where('wenshu_id', '=', $hulijilu_id)->execute();
    	DB::delete('zhuyuan_huli_wenshu_detail')->where('id', '=', $hulijilu_id)->execute();
    	DB::delete('zhuyuan_huli_wenshu_detail_content')->where('wenshu_id', '=', $hulijilu_id)->execute();
    	return true;
    }
}